/*
 * @Description:
 * @Version: 1.0
 * @Autor: lzc
 * @Date: 2022-06-14 16:58:32
 * @LastEditors: lzc
 * @LastEditTime: 2022-11-18 14:34:46
 */
#ifndef APP_H
#define APP_H

#include "general_config.h"
#include "key.h"
#include "string.h"
#include "adc.h"

//数据长度为 160 的压力垫
// #define Pressure_Pad_Manufacturers_160_Length
//数据长度为 1500 的压力垫
#define Pressure_Pad_Manufacturers_1500_Length

#define Pressure_Warning_Enable 1
#define Debug_Stop_Tump_MQTT 0   //屏蔽

#define Debug_Stop_Pump1 0  //屏蔽电机

#if Debug_Stop_Pump1  //屏蔽了电机1
#define Pump1ON gpio_bit_reset(PUMP1_GPIO_PORT, PUMP1_PIN);
#else
#define Pump1ON gpio_bit_set(PUMP1_GPIO_PORT, PUMP1_PIN);
#endif
 
#define Debug_Stop_Pump2 0  //屏蔽电机

#if Debug_Stop_Pump2  //屏蔽了电机1
#define Pump2ON gpio_bit_reset(PUMP2_GPIO_PORT, PUMP2_PIN);
#else
#define Pump2ON gpio_bit_set(PUMP2_GPIO_PORT, PUMP2_PIN);
#endif


//#define Pump2ON gpio_bit_set(PUMP2_GPIO_PORT, PUMP2_PIN);
#define Pump1Off gpio_bit_reset(PUMP1_GPIO_PORT, PUMP1_PIN);
#define Pump2Off gpio_bit_reset(PUMP2_GPIO_PORT, PUMP2_PIN);

#define Motor1ON gpio_bit_set(MOTOR1_GPIO_PORT, MOTOR1_PIN);
#define Motor2ON gpio_bit_set(MOTOR2_GPIO_PORT, MOTOR2_PIN);
#define Motor1Off gpio_bit_reset(MOTOR1_GPIO_PORT, MOTOR1_PIN);
#define Motor2Off gpio_bit_reset(MOTOR2_GPIO_PORT, MOTOR2_PIN);

#define Status_Pump1 PumpStatus.bit0
#define Status_Pump2 PumpStatus.bit1

#define Status_Limit1 LimitStatus.bit0
#define Status_Limit2 LimitStatus.bit1
#define Status_Limit3 LimitStatus.bit2
#define Status_Limit4 LimitStatus.bit3

#define rx2_buffersize 512 // wifi

#ifdef Pressure_Pad_Manufacturers_160_Length
#define rx1_buffersize 520 //压力垫数据
#endif

#ifdef Pressure_Pad_Manufacturers_1500_Length
#define rx1_buffersize 1536 //压力垫数据1536
#endif

#define PressValue_buffersize rx1_buffersize + 13

#define warn_LowPress_time 90 //单位秒，10分钟    //原90 //秒
#define warn_LowPress_longtime_beep 3600 //第一次报警，持续60min未充满,3600
#define warn_deviation 100     //气压偏差值

#define press_ad_exchage 1.0   //帕斯卡/press_ad_exchage=AD值
#define max_press 12000        //护理最大能充到这个值 20000
#define min_press 1000         //最小能充到这个值
#define sit_press 12000        //静态模式能充到这个值 16000
#define turn_press 12100       //翻身模式能充到这个值
#define turn_Stop 3000

// #define Warning_time_m 60 //气压过低倒计时，目前1分钟
#define WARNING_THRESHOLD 5000  // 低压报警阈值
#define WARNING_DURATION  600     // 第二次报警，持续低压10min后报警（秒）600
// #define RECOVERY_DELAY    5      // 恢复检测延迟时间（秒）

//#define Warning_time_m 60 //气压过低倒计时，目前1分钟

#define check_timeout 20     // s
#define CheckTimeDelay 14400 // s

#define InBedPress 4000 //在离床阈值

void usart_dma_config(void);
void usart1_config(void);
void usart2_config(void);
void timer5_init(uint16_t arr, uint16_t psc);
void usart2send(uint8_t *s, uint16_t lenth);
void Pump1Work(uint16_t pressure);
void Pump2Work(uint16_t pressure);
void ReceiveHdl(void);
void ModeHdl(void);
void SendHdl(void);
void PressValue_SendHandle(void);
void warn_hdl(void);
uint16_t calc_airpress(void);
void SendCalibrtion(void);
void WIFI_INIT(void);
// void TurnHdl(void);
void SetDown(void);
void bodystatus(void);
void getPressprofile(void);
void sendFirmwareInfo(void);
void sendHealthInfo(void);
void HealthSendHdl(void);
void sendpress_data(void);
void sendhumanbody_data(void);

void warn_data(void);

typedef struct Warning_Flag_T
{
    char Warn_Shoulder_Flag; // Shoulder_Flag
    char Warn_Back_Flag;     // Back_Flag
    char Warn_Hip_Flag;      // Hip_Flag
    char Warn_Leg_Flag;      // Leg_Flag

} Warning_Flag_t;

extern char count_Y_NoFlash;

extern uint8_t vave1_SetPosi, vave2_SetPosi;
extern uint8_t rx2_buff[rx2_buffersize];
extern uint8_t rx1_buff[rx1_buffersize];
extern uint8_t rx2_frame_OK;
extern uint8_t rx1_frame_OK;
extern uint16_t rx2count;
extern uint16_t rx1count;
extern uint8_t UartSendDelay;
extern uint16_t cycle_LeftTime, turn_LeftTime; // s
extern uint8_t pressTimeoutCnt;                // s
extern uint32_t turn_keeptime, nowave_keeptime;
extern Warning_Flag_t Warning_State;

#endif
